﻿using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;

namespace YBOT_Field_Control
{
    public class Game
    {
        #region Game and Scoring Variables

        OneWire g1w;                        //One Wire constructor
        XBee gXB;                           //Xbee Constructor
        Field_Control gFC;                  //Field Control Constructor
        LogWriter gLW = new LogWriter();    //Log Writer Constructor
        Time gTime = new Time();            //Time Constructor

        private string time =                //Time of day stamp
              DateTime.Now.Hour.ToString() + "_"
            + DateTime.Now.Minute.ToString() + "_"
            + DateTime.Now.Second.ToString();


        public TeamVariables red = new TeamVariables();            //New team 
        public TeamVariables green = new TeamVariables();        //New team

        public int autoModeTime = 30;   //Autonomous Mode time in secs
        public int midModeTime = 120;    //Mid Mode time + automode time
        public int matchNumber = 0;     //Current match number


        //Game flags
        public bool gameDone = false;       //True when game has finished
        public bool STOP = false;           //True when game has been stopped
        public bool autoMode = false;       //True when game is in autonomous mode
        public bool midMode = false;        //True when game is in middle game mode
        public bool endMode = false;        //True when game is in end of game mode
        public bool practiceMode = false;   //True when game is in practice mode

        //------------------------------------------------------------------------------------------------\\
        //Current year's game variables
        //------------------------------------------------------------------------------------------------\\

        private int autoballValue = 5;
        private int autoMax = 4;

        #endregion

        #region Constructors

        public Game()
        {
            this.g1w = new OneWire();
            this.gXB = new XBee();
            this.gFC = new Field_Control();
        }

        public Game(OneWire _g1w)
        {
            this.g1w = _g1w;
        }

        public Game(XBee _gXB)
        {
            this.gXB = _gXB;
        }

        public Game(Field_Control _gFC)
        {
            this.gFC = _gFC;
        }

        public Game(OneWire _g1w, XBee _gXB)
        {
            this.g1w = _g1w;
            this.gXB = _gXB;
        }

        public Game(OneWire _g1w, XBee _gXB, Field_Control _gFC)
        {
            this.g1w = _g1w;
            this.gXB = _gXB;
            this.gFC = _gFC;
        }

        #endregion

        //Initiate game class
        public void begin()
        {
            this.red.reset();                        //Reset Red variables
            this.green.reset();                      //Reset Green variables

            midMode = false;                    //Reset midGame flag
            endMode = false;                    //Reset endGame flag

            STOP = false;                       //Reset STOP flag
            gameDone = false;                   //Reset gameDone flag

        }

        /// <summary>
        /// Start a new game
        /// </summary>
        public void GameStartUp()
        {
            begin();        //reset variables and flags       
            this.gFC.StartUp();  //Start up field communications

            practiceMode = false;
            autoMode = true;

            GameLog("Game Started");

            //Ring Bell
            this.gFC.RingBell();

            //Turn on All Field Lights
            this.gFC.AllLights(true);

            //Turn off All Field Lights
            this.gFC.AllLights(false);

            //Reset latch status of each tower//
            this.gFC.AllTowersLatchReset();          
            GameLog("All Tower Latch Reset"); 

            //Turn on Robot Transmiters
            this.gFC.RobotTransmitters(true, true);
            GameLog("Transmitters On");

            GameLog("Game Start");

        }

        /// <summary>
        /// Start a new game
        /// </summary>
        public void GameStartUp(bool _autoMode, bool _practiceMode)
        {
            begin();
            this.gFC.StartUp();
            STOP = false;
            gameDone = false;
            autoMode = _autoMode;
            practiceMode = _practiceMode;

            GameLog("Game Started");

            //Ring Bell
            this.gFC.RingBell();

            //Turn on All Field Lights
            this.gFC.AllLights(true);

            //Turn off All Field Lights
            this.gFC.AllLights(false);

            GameLog("Transmitters On");

            GameLog("Game Start");

            //If practice mode
            if (practiceMode && _autoMode)
            {
                //Turn on Robot Transmiters
                this.gFC.RobotTransmitters(_autoMode, true);
            }
            else if (practiceMode)
            {
                //Turn on Robot Transmiters
                this.gFC.RobotTransmitters(false, true);
            }
            else
            {
                //Something to do here?
            }

        }

        /// <summary>
        /// End a Game
        /// </summary>
        public void GameShutDown()
        {
            //Turn off Transmitters
            this.gFC.RobotTransmitters(false, false);

            //Sound buzzer
            this.gFC.SoundBuzzer();

            GameLog("Towers Off");
            GameLog("Game End");
        }

        /// <summary>
        /// Main Game control :  enters and exits game modes
        /// </summary>
        public void MainGame()
        {

            //Do loop until the game is over or stopped
            while (!STOP && !gameDone)
            {
                //Game Flow code here
                if (!practiceMode)
                {
                    //If Automode enter AutoMode
                    if (autoMode && !midMode && !endMode) AutoMode();

                    //Do this between rounds
                    else if (autoMode && midMode && !endMode)
                    {
                        this.gFC.RobotTransmitters(3, false, true); //Turn on transmitter to Manual Mode
                        this.gFC.RingBell();                        //Ring bell
                        autoMode = false;                           //Set auto flag
                        GameLog("AutoMode Over");                   //Update Log
                        GameLog("Transmitters ON");                 //Update Log
                        this.gFC.AllLights(false);                  //Turn off all Lights
                    }

                    //If not autoMode and not Endmode enter MidMode
                    else if (!autoMode && midMode && !endMode) MidMode();

                    //If not autoMode and is midMode and endMode do this stuff between modes
                    else if (!autoMode && midMode && endMode)
                    {
                        this.gFC.AllLights(true);
                        this.gFC.RingBell();
                        midMode = false;
                        GameLog("MidMode Over");
                        this.gFC.AllLights(false);
                    }

                    //if not autoMode and not midMode and is endMode this it is endMode so run EndMode code
                    else if (!autoMode && !midMode && endMode) EndMode();
                }
                else
                {
                    //Place Practice Mode code here
                }

                Application.DoEvents();         //Do window's stuff
                return;                         //Return to control program
            }
        }

        public void AutoMode()
        {
            //AutoMode Code here
        }

        public void MidMode()
        {

            //Middle Mode code here
        }

        public void EndMode()
        {
            //End Mode code here
        }

        /// <summary>
        /// Writes text to log file
        /// </summary>
        /// <param name="text">Text as string</param>
        public void GameLog(string text)
        {
            string file = "\\Match " + matchNumber.ToString() + " - Log";
            string folder = "Matches\\" + "Match " + matchNumber.ToString();

            this.gLW.WriteLog(text, file, folder);
        }

        //------------------------------------------------------------------------------------------------\\
        //Current year's game methods
        //------------------------------------------------------------------------------------------------\\

    }
}
